import { ref } from 'vue'
export default class QueueTask {
  constructor() {
    this.list = [] //任务队列
    this.state = ref(0) //0空闲 1忙碌
    this.currentTask = null
  }
  doSome(some) {
    this.list.push(some)
    this.next()
  }
  next() {
    if (this.state.value == 0) {
      let fn = this.list.shift()
      if (fn) {
        this.state.value = 1
        const taskHandler = fn(() => {
          this.state.value = 0
          this.next()
        })
        if (taskHandler) this.currentTask = taskHandler
      } else {
        this.state.value = 0
      }
    }
  }
  start() {
    this.next()
  }
  stop() {
    this.state.value = 1
    this.currentTask?.stop()
  }
}
